package com.kamistoat.meimeichat.meimeichatclient.socket;

import com.kamistoat.meimeichat.meimeichatclient.application.UIServiceConnector;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Handler的基类
 */
public abstract class NettyClientBizHandler<T> extends SimpleChannelInboundHandler<T> {

    protected Logger logger = LoggerFactory.getLogger(NettyClientBizHandler.class);

    protected UIServiceConnector uiServiceConnector;

    public NettyClientBizHandler(UIServiceConnector uiServiceConnector) {
        this.uiServiceConnector = uiServiceConnector;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, T msg) throws Exception {
        channelRead(ctx.channel(), msg);
    }

    /**
     * Warning:子类只需要重写该方法
     */
    public abstract void channelRead(Channel channel, T msg);

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        logger.error("与服务器断开连接...");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        logger.info("关闭" + ctx.channel().id());
    }

}
